package com.dxp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxp.common.R;
import com.dxp.pojo.Category;
import com.dxp.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    //添加分类
    @PostMapping
    public R<String> addType(@RequestBody Category category, HttpServletRequest request){
        Long category1 = (Long) request.getSession().getAttribute("employee");
        category.setCreateUser(category1);
        category.setUpdateUser(category1);
        categoryService.save(category);
        return R.success("新增成功");
    }
    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        Page<Category> page1 = new Page<Category>(page, pageSize);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        categoryService.page(page1,queryWrapper);
        return R.success(page1);
    }
    //删除分类
    @DeleteMapping //不能带("/{ids}") 前端请求的url不是restful
    public R<String> delete(Long ids){
        categoryService.remove(ids);
        return R.success("删除分类信息成功");
    }
    //修改分类信息
    @PutMapping
    public R<String> update(@RequestBody Category category,HttpServletRequest request){
        Long o = (Long) request.getSession().getAttribute("employee");
        category.setUpdateUser(o);
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }
    //新增菜品展示菜品分类
    @GetMapping("/list")
    public R<List<Category>> list( Category category){
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,"type",category.getType());  //column参数也可写成Category::getType，（mp版本）
        queryWrapper.orderByAsc("sort").orderByDesc("update_time");
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }

}
